<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>多行文本转 JSON</title>
  <style>
    body {
      font-family: sans-serif;
    }

    textarea {
      width: 90%;
      height: 200px;
      margin: 10px auto;
      display: block;
      padding: 5px;
      border: 1px solid #ccc;
      resize: vertical;
      /* 允许垂直方向调整大小 */
    }

    button {
      display: block;
      margin: 10px auto;
      padding: 8px 16px;
      background-color: #4CAF50;
      color: white;
      border: none;
      cursor: pointer;
    }

    pre {
      width: 90%;
      margin: 10px auto;
      padding: 10px;
      border: 1px solid #ccc;
      background-color: #f0f0f0;
      white-space: pre-wrap;
      /* 允许换行 */
      overflow-x: auto;
      /* 水平滚动条，防止过长内容溢出 */
    }

    .btns {
      display: flex;
      justify-content: center;
      gap: 10px;
    }
  </style>
</head>

<body>

  <h2>多行文本转 JSON</h2>

  <textarea id="inputText" placeholder="在此输入多行文本，例如 JSON 字符串或 JavaScript 对象字面量"></textarea>
  <div class="btns">
    <button onclick="convertToOut()">转换为 待出库JSON</button>
    <button onclick="convertToPack()">转换为 封包JSON</button>
  </div>


  <pre id="outputCode"></pre>

  <script>
    function transformItemToPack(item) {
      const aimObject =
      {
        //调拨出库包装单从表数据
        "id": item.id,//id
        "thispackingQuantity": item.packingQuantity,//本次包装数量(子list装箱数量总和)
        "allocationPackingDetailsRecordsList"://包装单装箱明细记录list
          [
            {
              "packingCode": item.packingCode,//包装单号
              "packingDetailsCode": "JHD202412220002",//包装单明细单号
              "outCode": item.outCode,//出库单号
              "outDetailsCode": "",//出库明细单号
              "allocationCode": item.allocationCode,//调拨单号
              "materialCode": item.materialCode,//物料代码
              "materialName": item.materialName,//物料名称
              "batchNumber": item.batchNumber,//批次号
              "supplierCode": item.supplierCode,//供应商代码
              "supplierName": item.supplierName,//供应商名称
              "stockType": item.stockType,//库存类型 0现金 1寄售
              "packingQuantity": item.packingQuantity,//装箱数量
              "packgeType": "",//包材类型 0 纸箱	            1 木箱	            2 PE	            3 辅料
              "packgeCode": "",//包材编号
              "packgeName": "",//包材名称
              "grossWeight": "",//毛重
              "volume": "3",//体积
              "boxNumber": "1",//箱号
              "boxNumberId": "必填"//箱号ID
            }
          ]
      }
      return aimObject;
    }

    function transformItemToOut(item) {
      const aimObject =
      {
        "Applicationbatchnumber": "DBDH202412200015", //调拨申请批次号
        "date": "2024-08-16", //日期
        "Deliveryfactory": item.warehouseCode, //发货仓库代码
        "Deliveryfactoryremark": item.warehouseName, //发货仓库名称
        "Receivingfactory": "TJ01", //收货仓库代码
        "Receivingfactoryremark": "华北仓（天津）", //收货仓库名称
        "Deliveryinventory": item.zwarehouseCode, //发货子库代码
        "Deliveryinventoryremark": item.zwarehouseName, //发货子库名称
        "Receivingstock": "V823", //收货子库代码
        "Receivingstockremark": "天津配件合格库", //收货子库名称
        "Rowsnumber": "0001", //行项目
        "Materialcode": item.materialCode, //物料编号
        "Materialremark": item.materialName, //物料描述-名称
        "Company": item.unit, //单位
        "Quantity": "10", //数量
        "ZBY1": item.factoryCode, //工厂代码
        "ZBY2": "", //备用2
        "ZBY3": "", //备用3
        "ZBY4": "", //备用4
        "ZBY5": "", //备用5
        "ZBY6": "", //备用6
        "ZBY7": "", //备用7
        "ZBY8": "", //备用8
        "ZBY9": "", //备用9
        "ZBY10": "" //备用10
      }
      return aimObject;
    }

    function convertToOut() {
      const inputText = document.getElementById("inputText").value;
      const outputCode = document.getElementById("outputCode");

      try {
        // 尝试将输入解析为 JSON
        let jsonObject = JSON.parse(inputText);

        let aim = transformItemToOut(jsonObject);

        // 格式化 JSON 字符串，使用 4 个空格缩进
        const formattedJson = JSON.stringify(aim, null, 4);
        outputCode.textContent = formattedJson;
      } catch (error) {
        try {
          //如果json解析失败，尝试执行eval，将字符串转为js对象
          let evalResult = eval('(' + inputText + ')');
          if (typeof evalResult === 'object') {
            const formattedJson = JSON.stringify(evalResult, null, 4);
            outputCode.textContent = formattedJson;
          } else {
            outputCode.textContent = "输入格式错误，不是有效的 JSON 或 JavaScript 对象字面量！\n" + error;
          }
        } catch (evalError) {
          outputCode.textContent = "输入格式错误，不是有效的 JSON 或 JavaScript 对象字面量！\n" + evalError;
        }
      }
    }

    function convertToPack() {
      const inputText = document.getElementById("inputText").value;
      const outputCode = document.getElementById("outputCode");

      try {
        // 尝试将输入解析为 JSON
        let jsonObject = JSON.parse(inputText);

        let aim = transformItemToPack(jsonObject);

        // 格式化 JSON 字符串，使用 4 个空格缩进
        const formattedJson = JSON.stringify(aim, null, 4);
        outputCode.textContent = formattedJson;
      } catch (error) {
        try {
          //如果json解析失败，尝试执行eval，将字符串转为js对象
          let evalResult = eval('(' + inputText + ')');
          if (typeof evalResult === 'object') {
            const formattedJson = JSON.stringify(evalResult, null, 4);
            outputCode.textContent = formattedJson;
          } else {
            outputCode.textContent = "输入格式错误，不是有效的 JSON 或 JavaScript 对象字面量！\n" + error;
          }
        } catch (evalError) {
          outputCode.textContent = "输入格式错误，不是有效的 JSON 或 JavaScript 对象字面量！\n" + evalError;
        }
      }
    }



  </script>

</body>

</html>